#include "stdafx.h"



/*  David Jones  @CEDriverwiz
** Embedded MVP
** Sportronics
** Australia
** http://embedded101.com/Blogs/David-Jones
** The above contains a number of blogs on this topic.
*/

/*  Submits a new sensor and value to a  Micrsoft Azure Mobile Service
** Telemetry  is based upon the ToDo sample app that can be generated when an Azure Mobile Service is created
** Submitted sensor is defined below in SENSOR.  The  value to be set is defined below in VALUE
*/



// Azure Mobile Service address
// You can find this in your service dashboard
char AMW_SERVICE[SIZE_OF_SERVERNAME]; // = AMW_SERVICE;

// Azure Mobile Service table name
// The name of the table you created
//const char *table_name = TABLE;

// Azure Mobile Service Application Key
// You can find this key in the 'Manage Keys' menu on the dashboard
char APP_KEY[SIZE_OF_APPKEY]; // = APP_KEY;

char TABLE[SIZE_OF_TABLENAME]; // = "telemetry2";

char buffer[MAX_PATH];
char *  Sensor = "SENSOR";
int Value = 0;

/*
** Send an HTTP POST request to the Azure Mobile Service data API
*/
void Send_POST_Request(char * table_name, char * sensor, int ivalue, float fvalue, BOOL isInt)
{
	output.println("\nconnecting...");


	if (client.connect((char *)AMW_SERVICE, 80)) {

		output.print("sending ");
		output.println(sensor);

		// POST URI
		sprintf(buffer, "POST /tables/%s?embedded=1 HTTP/1.1", table_name);
		client.println(buffer);
		output.println(buffer);

		// Host header
		sprintf(buffer, "Host: %s", AMW_SERVICE);
		client.println(buffer);
		output.println(buffer);

		// Azure Mobile Services application key
		sprintf(buffer, "X-ZUMO-APPLICATION: %s", APP_KEY);
		client.println(buffer);
		output.println(buffer);

		// JSON content type
		client.println("Content-Type: application/json");
		output.println("Content-Type: application/json");

		//Content
		if (isInt)
		{
			output.println(ivalue);
			// POST body
			sprintf(buffer, "{\"sensor\":\"%s\",\"value\":%d}", sensor, ivalue);
		}
		else
		{
			output.println_f(fvalue);
			// POST body
			sprintf(buffer, "{\"sensor\":\"%s\",\"value\":%.*f}", sensor, fvalue);
		}

		// Content length
		client.print("Content-Length: ");
		client.println(strlen(buffer));
		output.print("Content-Length: ");
		output.println(strlen(buffer));


		// End of headers
		client.println();
		output.println();

		// Request body
		client.println(buffer);
		output.println(buffer);
		client.SentHTTPMessage();
	}
	else {
		output.println("connection failed");
	}
}

extern "C"  void Init_AZMS(char* azms, char * table, char * appKey)
{
	strncpy(AMW_SERVICE, azms, (size_t)SIZE_OF_SERVERNAME);
	strncpy(APP_KEY, appKey, (size_t) SIZE_OF_APPKEY);
	strncpy(TABLE, table, (size_t) SIZE_OF_TABLENAME);
}


/*
** Setup
*/
void setup()
{
	output.begin(9600);
	while (!output.available()) {
		; // wait for serial port to connect.
	}
}

extern "C" BOOL POST_TelemetryValue(char * table_name,char * name, int value, bool enableOutput)
{
	output.begin(0);
	if (enableOutput)
		output.Enable();
	else
		output.Disable();


	Send_POST_Request(table_name, name,value,0,true);
	return true;
}

extern "C" BOOL POST_TelemetryValueFlt(char * table_name, char * name, float value, bool enableOutput)
{
	output.begin(0);
	if (enableOutput)
		output.Enable();
	else
		output.Disable();

	Send_POST_Request(table_name, name, 0,value, false);
	return true;
}






